package com.coolgor.axnut.web.wechat.fwh;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coolgor.airvow.Net.HttpServletRequestUtil;
import com.coolgor.axnut.dto.auth.LocalUserExecution;
import com.coolgor.axnut.dto.auth.RoleExecution;
import com.coolgor.axnut.dto.wechat.qyh.QYHAccessTokenExecution;
import com.coolgor.axnut.dto.wechat.qyh.QYHUserExecution;
import com.coolgor.axnut.entity.auth.LocalUser;
import com.coolgor.axnut.entity.auth.LoginInfo;
import com.coolgor.axnut.entity.auth.Role;
import com.coolgor.axnut.entity.wechat.qyh.QYHUser;
import com.coolgor.axnut.enums.auth.GenderEnum;
import com.coolgor.axnut.enums.auth.LocalUserStateEnum;
import com.coolgor.axnut.enums.auth.LoginStateEnum;
import com.coolgor.axnut.enums.auth.RoleStateEnum;
import com.coolgor.axnut.enums.wechat.qyh.QYHAccessTokenStateEnum;
import com.coolgor.axnut.enums.wechat.qyh.QYHUserStateEnum;
import com.coolgor.axnut.service.auth.LocalUserService;
import com.coolgor.axnut.service.auth.LoginInfoService;
import com.coolgor.axnut.service.auth.RoleService;
import com.coolgor.axnut.service.wechat.qyh.QYHAccessTokenService;
import com.coolgor.axnut.service.wechat.qyh.QYHUserService;
import com.coolgor.axnut.util.wechat.qyh.WechatQYHUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 获取关注公众号之后的微信用户信息的接口，如果在微信浏览器里访问
 * https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd7f6c5b8899fba83&redirect_uri=http://o2o.yitiaojieinfo.com/o2o/wechatlogin/logincheck&role_type=1&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
 * https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf5124501c044efec&redirect_uri=http://www.lordforce.com/axnut/wechatlogin/logincheck&role_type=1&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
 * https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxef31de7a23359e19&redirect_uri=http://www.lordforce.com/axnut/wxqyhlogin/logincheck&response_type=code&scope=snsapi_base&state=1#wechat_redirect
 * 则这里将会获取到code,之后再可以通过code获取到access_token 进而获取到用户信息
 * 
 * @author CoolGor
 *
 */
@Controller
@RequestMapping("wxfwhlogin")
public class WechatFWHLoginController {

	private static Logger log = LoggerFactory.getLogger(WechatFWHLoginController.class);
	
	@Autowired
	private QYHAccessTokenService qyhAccessTokenService;
	@Autowired
	private QYHUserService qyhUserService;
	@Autowired
	private LocalUserService localUserService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private LoginInfoService loginInfoService;
	

	@RequestMapping(value="/logincheck", method={RequestMethod.GET}, produces= {"application/json;charset=UTF-8"})
	@ResponseBody
	//@JsonIgnoreProperties(ignoreUnknown = true)//用ObjectMapper转换json对象时，忽略不存在的字段。
	public String doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		log.debug("WXFWH login get...");
		// 获取微信公众号传输过来的code,通过code可获取access_token,进而获取用户信息
		String code = request.getParameter("code");
		// 这个state可以用来传我们数据库中约定的record ID
		String state = request.getParameter("state");
		String[] states = state.split("/");
		String recordId = state;
		System.out.println(code);
		System.out.println(state);
		return modelMap.toString();
	}
	
	
	
}
